<?php

namespace App\AdminModule\Presenters;

use \Nette\Application\UI\Form,
    App\Model\Dao\Page,
    App\Model\Dao\Photo,
    \App\Component\PhotoList,
    \Grido, 
    \Grido\Grid, 
    \Grido\Components\Columns,
    \Grido\DataSources\Doctrine;

/**
 * Page presenter.
 */
class PagePresenter extends BasePresenter {
 
  /** @var \Kdyby\Doctrine\EntityManager @inject */
  public $em;
  
  private $page;
  
  /**
   * Add action
   * @param int $id
   * @return Page
   */
  public function actionAdd($id){
    if(!isset($id)){
      return;
    }
    
    $this->page = $this->em->getDao(Page::getClassName())->find($id);
  }
  
  /**
   * Add page form
   * @param String $name
   * @return \Nette\Application\UI\Form
   */
  public function createComponentAddPageForm($name){
    $form = new Form();
    $form->addText('title', 'Titulek')->addRule(Form::FILLED, 'Titulek musí být vyplněn');
    $form->addText('link', 'Link')->addRule(Form::FILLED, 'Link musí být vyplněn');
    $form->addText('description', 'Popis')->addRule(Form::FILLED, 'Popis musí být vyplněn');
    $form->addText('keywords', 'Klíčová slova')->addRule(Form::FILLED, 'Klíčová slova musí být vyplněna');
    $form->addTextArea('content', 'Obsah')->addRule(Form::FILLED, 'Titulek musí být vyplněn');

    if($this->page){
      $form['title']->setDefaultValue($this->page->getTitle());
      $form['link']->setDefaultValue($this->page->getLink());
      $form['description']->setDefaultValue($this->page->getDescription());
      $form['keywords']->setDefaultValue($this->page->getKeywords());
      $form['content']->setDefaultValue($this->page->getContent());
    }
    
    $form->addSubmit('send');
    $form->onSuccess[] = callback($this, 'addPageFormSuccess');
    
    return $form;
  }
  
  /**
   * Handle for page delete
   * @param int $id
   */
  public function handleDelete($id){
    $page = $this->em->getDao(Page::getClassName())->find($id);
    
    if($page){
      $this->em->remove($page);
      $this->em->flush();
    }
    
    $this->redirect('Page:default');
  }
  
  /**
   * Page grid
   * @param String $name
   * @return \Grido\Grid
   */
  public function createComponentPageGrid($name){
    $qb = $this->em->createQueryBuilder()
      ->addSelect('p.id AS id')
      ->addSelect('p.title AS title')
      ->addSelect('p.link AS link')
      ->from(Page::getClassName(), ' p');

    $pages =  $qb->getQuery()->getArrayResult();

    $g = new Grid($this, $name);

    // Grido data
    $g->model = $pages;
    
    $g->addColumnText('id', 'ID')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('title', 'Titulek')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('link', 'Link')
        ->setSortable()
        ->setFilterText();
    
    // Create edit button
    $g->addActionHref('edit', 'Edit', 'Page:add')
        ->setIcon('pencil')->setPrimaryKey('id');
    $g->addActionHref('delete', 'Smazat', 'delete!')
        ->setIcon('remove')->setPrimaryKey('id');
    
    // Return grido
    $g->filterRenderType = Grido\Components\Filters\Filter::RENDER_INNER;
    $g->setExport();
    return $g;
  }
  
  /**
   * Execute page form
   * @param Form $form
   */
  public function addPageFormSuccess($form){
    $values = $form->getValues();

    if(isset($this->page)){
      $this->page->setTitle($values['title']);
      $this->page->setLink($values['link']);
      $this->page->setDescription($values['description']);
      $this->page->setKeywords($values['keywords']);
      $this->page->setContent($values['content']);
      $this->em->persist($this->page);
    }else{
      $page = new Page();
      $page->setTitle($values['title']);
      $page->setLink($values['link']);
      $page->setDescription($values['description']);
      $page->setKeywords($values['keywords']);
      $page->setContent($values['content']);
      $this->em->persist($page);
    }

    $this->em->flush();

    $this->flashMessage('Stránka byla uložena', 'success');
    $this->redirect('Page:default');
  }
  
  /**
   * List of photos
   * @param String $name
   * @return PhotoList
   */
  public function createComponentPhotoList($name){
    $photoList = new PhotoList();
    return $photoList->init($this->em->getDao(Photo::getClassName())->findBy(array(), array('id' => 'desc')));
  }
}
